import { create } from 'zustand';

import { getItem, removeItem, setItem } from '@/utils/storage';
import { StorageEnum } from '@/types/enum';

type DatasetType = {
  expandKeys: string[];
  // selectedKey: string;
};
type SettingStore = {
  datasets: DatasetType;
  // 使用 actions 命名空间来存放所有的 action
  actions: {
    setDataset: (settings: DatasetType) => void;
    clearDataset: () => void;
  };
};

const useDatasetsStore = create<SettingStore>((set) => ({
  datasets: getItem<DatasetType>(StorageEnum.Dataset) || {
    expandKeys: [],
    // selectedKey: "",
  },
  actions: {
    setDataset: (datasets) => {
      set({ datasets });
      setItem(StorageEnum.Dataset, datasets);
    },
    clearDataset() {
      removeItem(StorageEnum.Dataset);
    },
  },
}));

export const useDatasets = () => useDatasetsStore((state) => state.datasets);
export const useDatasetsActions = () => useDatasetsStore((state) => state.actions);
